/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.bean.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.basics.i18n.I18nMessage;
import com.genia.toolbox.persistence.bean.CrossSessionObject;
import com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameter;
import com.genia.toolbox.spring.transaction.bean.impl.EnlistableResourceImmutableValue;

/**
 * implementation of {@link PersistableApplicationParameter}.
 * 
 * @param <TYPE>
 *          the type of the value
 */
public class PersistableApplicationParameterImpl<TYPE>
    extends EnlistableResourceImmutableValue<TYPE>
    implements PersistableApplicationParameter<TYPE>, BeanNameAware
{

  /**
   * the categorie of the {@link PersistableApplicationParameter}.
   */
  private I18nMessage category;

  /**
   * the name of the {@link PersistableApplicationParameter}.
   */
  private I18nMessage name;

  /**
   * the {@link List} of regular expression this parameter must verify in case
   * of a simple parameter.
   */
  private List<String> validationRegularExpressions = new ArrayList<String>();

  /**
   * the class of the type of the parameter.
   */
  private Class<TYPE> parameterClass;

  /**
   * the name of this bean in the spring configuration. It will be used to link
   * to the persistent
   * {@link com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter}.
   */
  private String referenceName;



  /**
   * getter for the category property.
   * 
   * @return the category
   */
  public I18nMessage getCategory()
  {
    return category;
  }



  /**
   * getter for the name property.
   * 
   * @return the name
   */
  public I18nMessage getName()
  {
    return name;
  }



  /**
   * getter for the parameterClass property.
   * 
   * @return the parameterClass
   */
  public Class<TYPE> getParameterClass()
  {
    return parameterClass;
  }



  /**
   * getter for the referenceName property.
   * 
   * @return the referenceName
   */
  public String getReferenceName()
  {
    return referenceName;
  }



  /**
   * returns the value of the
   * {@link com.genia.toolbox.projects.toolbox_basics_project.bean.ApplicationParamete}.
   * 
   * @return the value of the
   *         {@link com.genia.toolbox.projects.toolbox_basics_project.bean.ApplicationParamete}
   * @see com.genia.toolbox.projects.toolbox_basics_project.bean.ApplicationParameter#getValue()
   */
  @Transactional(readOnly = true)
  public TYPE getValue()
  {
    TYPE value = getObject();
    if (value instanceof CrossSessionObject && ((CrossSessionObject) value).getCurrentObject() == null) {
      return null;
    }
    return value;
  }



  /**
   * setter for the beanName property.
   * 
   * @param beanName
   *          the name of the bean.
   * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
   */
  public void setBeanName(String beanName)
  {
    setReferenceName(beanName);
  }



  /**
   * setter for the category property.
   * 
   * @param category
   *          the category to set
   */
  public void setCategory(I18nMessage category)
  {
    this.category = category;
  }



  /**
   * setter for the name property.
   * 
   * @param name
   *          the name to set
   */
  public void setName(I18nMessage name)
  {
    this.name = name;
  }



  /**
   * setter for the parameterClass property.
   * 
   * @param parameterClass
   *          the parameterClass to set
   */
  public void setParameterClass(Class<TYPE> parameterClass)
  {
    this.parameterClass = parameterClass;
  }



  /**
   * setter for the referenceName property.
   * 
   * @param referenceName
   *          the referenceName to set
   */
  public void setReferenceName(String referenceName)
  {
    this.referenceName = referenceName;
  }



  /**
   * set the current value of the
   * {@link com.genia.toolbox.projects.toolbox_basics_project.bean.ApplicationParameter}.
   * 
   * @param value
   *          the value of the
   *          {@link com.genia.toolbox.projects.toolbox_basics_project.bean.ApplicationParameter}.
   */
  @Transactional
  public void setValue(TYPE value)
  {
    setObject(value);
  }



  /**
   * getter for the validationRegularExpressions property.
   * 
   * @return the validationRegularExpressions
   */
  public List<String> getValidationRegularExpressions()
  {
    return validationRegularExpressions;
  }



  /**
   * setter for the validationRegularExpressions property.
   * 
   * @param validationRegularExpressions
   *          the validationRegularExpressions to set
   */
  public void setValidationRegularExpressions(List<String> validationRegularExpressions)
  {
    this.validationRegularExpressions = validationRegularExpressions;
  }



  /**
   * setter for the validationRegularExpression property.
   * 
   * @param validationRegularExpression
   *          the validationRegularExpressions to set
   */
  public void setValidationRegularExpression(String validationRegularExpression)
  {
    this.validationRegularExpressions = Arrays.asList(validationRegularExpression);
  }

}
